package com.mmx.baseservice.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.mmx.baseservice.common.annotation.parameterverify.ParameterVerify;
import com.mmx.baseservice.common.utils.CommUtil;
import com.mmx.baseservice.common.utils.FileUtil;
import com.mmx.baseservice.common.utils.PathUtil;
import com.mmx.baseservice.common.utils.page.pojo.PageInfo;
import com.mmx.baseservice.common.utils.page.pojo.PageResult;
import com.mmx.baseservice.common.utils.page.util.PageUtil;
import com.mmx.baseservice.common.utils.returnbody.ReturnBody;
import com.mmx.baseservice.common.websocket.util.WsSendUtil;
import com.mmx.baseservice.system.pojo.SysDbBackup;
import com.mmx.baseservice.system.service.SysDbBackupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

/**
 * @author mmx generator
 * @since 2021-02-05
 */
@ApiIgnore
@Api(tags = "系统-数据库备份")
@RestController
@RequestMapping("system/sysDbBackup")
public class SysDbBackupController {

    @Resource
    private SysDbBackupService sysDbBackupService;
    @Resource
    private HttpServletRequest request;
    @Value("${backup.mysql.ip:}")
    private String mysqlIp;
    @Value("${backup.mysql.host:}")
    private String mysqlHost;
    @Value("${backup.mysql.database:}")
    private String mysqlDatabase;
    @Value("${spring.datasource.druid.username}")
    private String mysqlUser;
    @Value("${spring.datasource.druid.password}")
    private String mysqlPassword;

    @ApiOperation("数据备份")
    @PostMapping("admin/backUp")
    public String backUp() {
        return sysDbBackupService.add("手动备份");
    }


    @ApiOperation("备份列表")
    @PostMapping("admin/list")
    public String list(PageInfo pageInfo) {
        pageInfo.setTimeScreen(SysDbBackup.CREATE_TIME);
        PageResult<SysDbBackup> pageResult = new PageUtil<SysDbBackup>(pageInfo).startPage(sysDbBackupService::page);
        return ReturnBody.success(pageResult);
    }


    @ApiOperation("删除备份")
    @PostMapping("admin/delete")
    @ParameterVerify(notNull = "id")
    public String delete(Integer id) {
        // 查询删除的备份信息
        SysDbBackup sysDbBackup = sysDbBackupService.getById(id);
        if (null == sysDbBackup) {
            return ReturnBody.error();
        }
        // 删除备份
        if (!sysDbBackupService.removeById(id)) {
            return ReturnBody.error();
        }
        // 删除源文件
        FileUtil.delete(sysDbBackup.getPath());
        return ReturnBody.success();
    }


    @ApiOperation("恢复备份")
    @PostMapping("admin/restore")
    @ParameterVerify(notNull = "id")
    public void restore(Integer id, String uid) throws IOException, InterruptedException, ExecutionException {
        SysDbBackup sysDbBackup = sysDbBackupService.getById(id);
        String cmd2 = "cmd /c" + " mysql" +
                " -h " + mysqlIp +
                " -P" + mysqlHost +
                " -u" + mysqlUser +
                " -p" + mysqlPassword +
                " -D" + mysqlDatabase +
                " < " + PathUtil.getFullRealPath(request) + sysDbBackup.getPath();
        boolean flag = CommUtil.runCmd(cmd2);
        JSONObject result = new JSONObject();
        result.put("uid", uid);
        if (!flag) {
            result.put("result", false);
        }else{
            result.put("result", true);
        }
        WsSendUtil.sendMessage("/queue/restore", result.toJSONString());
    }

}
